"""
  此文件对提取的跟驰数据进行测试（测试s、v、acc）
"""

# !处理路径导入问题（添加绝对路径）！！！
import sys
import os
CODE_INTERNAL_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')) # 生成Code文件夹内部对应的绝对路径
sys.path.append(CODE_INTERNAL_PATH)

# 导入内部包
from utils.read_data import read_extract_follow_data
from utils.draw_data import draw_line_chart

# 文件路径常量
FILE_PATH_I80_1_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/1. 跟随数据/trajectories-0400-0415_follow.txt" # 2352 200 14
FILE_PATH_I80_2_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/1. 跟随数据/trajectories-0500-0515_follow.txt" # 3420 200 14
FILE_PATH_I80_3_to = "../../../Data/Ngsim数据集/I80数据集/3. 提取数据/1. 跟随数据/trajectories-0515-0530_follow.txt" # 3912 200 14
FILE_PATH_101_1_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/1. 跟随数据/trajectories-0750am-0805am_follow.txt" # 2636 200 14
FILE_PATH_101_2_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/1. 跟随数据/trajectories-0805am-0820am_follow.txt" # 3651 200 14
FILE_PATH_101_3_to = "../../../Data/Ngsim数据集/101数据集/3. 提取数据/1. 跟随数据/trajectories-0820am-0835am_follow.txt" # 4165 200 14

def extract_test_data(one_car_data, front_s_idx=5, self_s_idx=6, front_v_idx=7, self_v_idx=8, front_acc_idx=9, self_acc_idx=10, dis_idx=12):
  # 提取所有s值
  front_s = [row[front_s_idx] for row in one_car_data]
  self_s = [row[self_s_idx] for row in one_car_data]

  # 提取所有v值
  front_v = [row[front_v_idx] for row in one_car_data]
  self_v = [row[self_v_idx] for row in one_car_data]

  # 提取所有acc值
  front_acc = [row[front_acc_idx] for row in one_car_data]
  self_acc = [row[self_acc_idx] for row in one_car_data]

  # 提取所有车头间距值
  distance = [row[dis_idx] for row in one_car_data]

  return front_s, self_s, front_v, self_v, front_acc, self_acc, distance

def draw_chart(front_s, self_s, front_v, self_v, front_acc, self_acc, distance):
  data_x = [i for i in range(len(front_s))]

  # 绘制s
  datas_y = [{"data":front_s, "des":"头车车辆位置"}, {"data":self_s, "des":"自车车辆位置"}]
  labels = {"x":"时间（单位:0.1s）", "y":"位置（单位：m）"}
  draw_line_chart(data_x, datas_y, labels, "车辆纵向坐标")

  # 绘制v
  datas_y = [{"data":front_v, "des":"头车车辆速度"}, {"data":self_v, "des":"自车车辆速度"}]
  labels = {"x":"时间（单位:0.1s）", "y":"速度（单位：m/s）"}
  draw_line_chart(data_x, datas_y, labels, "车辆速度")

  # 绘制acc
  datas_y = [{"data":front_acc, "des":"头车车辆加速度"}, {"data":self_acc, "des":"自车车辆加速度"}]
  labels = {"x":"时间（单位:0.1s）", "y":"加速度（单位：m/s^2）"}
  draw_line_chart(data_x, datas_y, labels, "车辆加速度")

  # 绘制车头间距
  datas_y = [{"data":distance, "des":"车头间距"}]
  labels = {"x":"时间（单位:0.1s）", "y":"车头间距（单位：m）"}
  draw_line_chart(data_x, datas_y, labels, "车头间距")

if __name__ == "__main__":
  # 读取数据
  follow_data = read_extract_follow_data(FILE_PATH_101_1_to)
  print(len(follow_data),len(follow_data[0]),len(follow_data[0][0]))

  # 取一组测试数据
  test_data = follow_data[30]

  # 提取测试数据
  front_s, self_s, front_v, self_v, front_acc, self_acc, distance = extract_test_data(test_data)

  # 绘图
  draw_chart(front_s, self_s, front_v, self_v, front_acc, self_acc, distance)